﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class SearchBug : System.Web.UI.Page
{
    User currenuser = UserManagement.GetCurrentUser();

    protected void Page_Load(object sender, EventArgs e)
    {
        SetTextBoxHints(searchtxt, "Type your search term here and click search");
        if (Request.QueryString["q"] != null)
            searchtxt.Text = Request.QueryString["q"];

        ltrSearchAdvanced.Text = "hide";
        if (!IsPostBack)
        {
            ddlProjectName.Items.Add("All");
            ddlBugID.Items.Add("All");
            ddlRole.Items.Add("All");
            ddlActive.Items.Add("All");

            ddlProjectName.SelectedValue = "All";
            ddlBugID.SelectedValue = "All";
            ddlRole.SelectedValue = "All";
            ddlActive.SelectedValue = "All";
            // add project name dropdown list
            var ProjectID = from item in ApplicationGlobalVariables.KillDaBugsDatabase.User_Projects.Where(i => i.Username == currenuser.Username)
                            select item.Project;
            ddlProjectName.Items.Clear();
            foreach (var item in ProjectID)
            {
                ListItem lt = new ListItem(item.Name, item.ProjectId.ToString());
                ddlProjectName.Items.Add(lt);
            }

            //add bug ID dropdown list
            var bugid = from item in ApplicationGlobalVariables.KillDaBugsDatabase.User_Bugs.Where(i => i.Username == currenuser.Username)
                        select item.Bug.BugId;
            ddlBugID.Items.Clear();
            foreach (var item in bugid)
            {
                ddlBugID.Items.Add(item.ToString());
            }

            //add Role dropdown list
            foreach (var item in EnumUtility.EnumToList<UserManagement.Role>())
            {
                ddlRole.Items.Add(EnumUtility.GetEnumNiceName(item));
            }
        }
    }

    protected void lbtnAdvancedSearch_Click(object sender, EventArgs e)
    {
        if (lbtnAdvancedSearch.Text == "Advanced Search")
        {
            ltrSearchAdvanced.Text = "";
            lbtnAdvancedSearch.Text = "Simple Search";
        }
        else
        {
            ltrSearchAdvanced.Text = "hide";
            lbtnAdvancedSearch.Text = "Advanced Search";

            // reset default options
            ddlProjectName.SelectedValue = "All";
            ddlBugID.SelectedValue = "All";
            ddlActive.SelectedValue = "All";
            ddlRole.SelectedValue = "All";
        }
    }

    protected void rdBug_CheckedChanged(object sender, EventArgs e)
    {
        Response.Redirect("/SearchBug.aspx?q=" + HttpUtility.HtmlEncode(searchtxt.Text));
    }

    private void SetTextBoxHints(TextBox textBox, string defaultText)
    {
        textBox.Attributes.Add("onfocus", "clearText(this,'" + defaultText + "')");
        textBox.Attributes.Add("onblur", "resetText(this,'" + defaultText + "')");
        if (textBox.Text == "")
        {
            textBox.Text = defaultText;
            textBox.ForeColor = System.Drawing.Color.Silver;
        }
        else if (textBox.Text == defaultText)
        {
            textBox.ForeColor = System.Drawing.Color.Silver;
        }
        else
        {
            textBox.ForeColor = System.Drawing.Color.Black;
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string text = searchtxt.Text.Trim();
        var user = from item in ApplicationGlobalVariables.KillDaBugsDatabase.Users
                   where item.Username.Contains(text) || item.Fullname.Contains(text) || item.Email.Contains(text) || item.Organization.Contains(text)
                   select new
                   {
                       item.Username,
                       item.Fullname,
                       item.IsActive,
                       //  item.User.RecentLoginDate,
                       item.AvatarImage,
                       item.Email,
                       item.Organization,
                       //  item.User.EnableEmailNotification,
                       //   item.BugsPerPage,
                       item.Role
                   };
        LVDisplay.DataSource = user;
        LVDisplay.DataBind();
    }
}